﻿/*
 * Created by SharpDevelop.
 * User: V.I.Evgenyev
 * Date: 02.09.2010
 * Time: 11:44
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Configuration;
using System.Reflection;
using System.IO;

namespace TreeDB
	
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
	

		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
	//		start_app_config();
			//
	
		}
			
			
//------диалог открытия БД ------------------------------------------------------------------------
		void ToolStripButton1Click(object sender, EventArgs e)
		{
			if (openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				treeDB_open (db_file_name.Value);
			}
			
		
		}
//--------создание новой БД------------------------------------------------------------------------
		void ToolStripButton3Click(object sender, EventArgs e)
		{
			create_DB ();
			db_file_name.Value = "NewDB.3db";
			treeDB_open (db_file_name.Value);
			
		}
//--------выбор узла ------------------------------------------------------------------------------
		void TreeView1AfterSelect(object sender, TreeViewEventArgs e)
						
		{
			TreeNode ns = treeView1.SelectedNode;
			read_data (ns.Name);
			toolStripStatusLabel1.Text = "Node:" + ns.Name;
		
		}
//---------редактирование имени узла --------------------------------------------------------------
		void TreeView1AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
		{
			
			try
			{
			TreeNode ns = treeView1.SelectedNode;
			DB_name_edit (ns.Name, e.Label.ToString(), db_file_name.Value);
			}
			catch
			{
				
			}
	
		}
//---------выбор файла БД для открытия-------------------------------------------------------------
		void OpenFileDialog1FileOk(object sender, System.ComponentModel.CancelEventArgs e)
		{
			
			db_file_name.Value = openFileDialog1.FileName;

		}
//----------добавить узел--------------------------------------------------------------------------
		void AsNextToolStripMenuItemClick(object sender, EventArgs e)
		{
			add_new_item (0, 2);
		}
//----------добавить узел--------------------------------------------------------------------------
		void AsChildToolStripMenuItemClick(object sender, EventArgs e)
		{
			add_new_item (1, 2);
		}
//----------добавить узел--------------------------------------------------------------------------
		void AsNextToolStripMenuItem1Click(object sender, EventArgs e)
		{
			add_new_item (0, 1);
		}
//----------добавить узел--------------------------------------------------------------------------
		void AsChildToolStripMenuItem1Click(object sender, EventArgs e)
		{
			add_new_item (1, 1);
		}
//-----------удалить узел--------------------------------------------------------------------------
		void DeleteToolStripMenuItemClick(object sender, EventArgs e)
		{
			TreeNode ns = treeView1.SelectedNode;
			DB_delete_node (Convert.ToInt32 (ns.Name));
		}
//-----------чтение файла конфигурации при загрузке программы--------------------------------------
		void MainFormShown(object sender, EventArgs e)
		{
			start_app_config ();
		}
//----------сохранение конфигурации при закрытии программы-----------------------------------------
		void MainFormFormClosing(object sender, FormClosingEventArgs e)
		{
			save_app_config ();
		}
//-------------------- Поиск узлов ----------------------------------------------------------------		
		void ToolStripButton4Click(object sender, EventArgs e)
		{
		Find_Form find_Form = new Find_Form();
		find_Form.Owner = this;
		find_Form.ShowDialog ();
		}
//-------------------- О прогроамме ---------------------------------------------------------------
		void ToolStripButton7Click(object sender, EventArgs e)
		{
			Assembly assem = Assembly.GetEntryAssembly();
			AssemblyName assemName = assem.GetName();
			Version ver = assemName.Version;
			string info = "TreeDB.NET";
			string info2 = "ver. " + ver.ToString();
			MessageBox.Show (info2, info);
		}
//-------------------- Установка иконки для узла --------------------------------------------------
		void DocumentToolStripMenuItemClick(object sender, EventArgs e)
		{
			TreeNode ns = treeView1.SelectedNode;
			set_icon (ns.Name, 2);
		}
//-------------------- Установка иконки для узла --------------------------------------------------		
		void FolderToolStripMenuItemClick(object sender, EventArgs e)
		{
			TreeNode ns = treeView1.SelectedNode;
			set_icon (ns.Name, 1);
		}
//-------------------- Установка иконки для узла --------------------------------------------------		
		void BellToolStripMenuItemClick(object sender, EventArgs e)
		{
			TreeNode ns = treeView1.SelectedNode;
			set_icon (ns.Name, 3);
		}
//-------------------- Установка иконки для узла --------------------------------------------------		
		void BroomToolStripMenuItemClick(object sender, EventArgs e)
		{
			TreeNode ns = treeView1.SelectedNode;
			set_icon (ns.Name, 4);
		}
//-------------------- Установка иконки для узла --------------------------------------------------		
		void ClockToolStripMenuItemClick(object sender, EventArgs e)
		{
			TreeNode ns = treeView1.SelectedNode;
			set_icon (ns.Name, 5);
		}
//-------------------- Установка иконки для узла --------------------------------------------------		
		void CrossToolStripMenuItemClick(object sender, EventArgs e)
		{
			TreeNode ns = treeView1.SelectedNode;
			set_icon (ns.Name, 6);
		}
//-------------------- Перетаскивание узла --------------------------------------------------------
		void TreeView1DragDrop(object sender, DragEventArgs e)
		{
			Point pt = new Point(e.X, e.Y);
    		pt = treeView1.PointToClient(pt);
   	   		TreeNode nodeSource = (TreeNode)e.Data.GetData(typeof(TreeNode));
    		TreeNode nodeTarget = treeView1.GetNodeAt(pt);
    		
    		string message = "Move node: '" + nodeSource.Text + "' to node: '" + nodeTarget.Text + "'?";
            string caption = "Move node";
			MessageBoxButtons buttons = MessageBoxButtons.YesNo;
			DialogResult result;
			result = MessageBox.Show(message, caption, buttons);
			if (result == System.Windows.Forms.DialogResult.Yes)
			{
				if (nodeSource !=null & nodeTarget != null)
 		   		{
       			if (ID_check (nodeSource.Name.ToString()))
    				{
// вносим изменения в базу данных       				
       				int id_new = max_id() + 1;
       				Move_node (nodeSource.Name.ToString(), nodeTarget.Name.ToString(), id_new);
// вносим изменения в дерево узлов       	
	   				treeView1.SelectedNode.Name = id_new.ToString();
	   				nodeTarget.Nodes.Add((TreeNode)nodeSource.Clone());
      				treeView1.SelectedNode.Remove();
	   				nodeTarget.Expand();
  			}
   		}
				
			}
    //		MessageBox.Show ("Move node: '" + nodeSource.Text + "' to node: '" + nodeTarget.Text + "'");
 //   		
		}
//------------------Эффект перетаскивания----------------------------------------------------------
		void TreeView1DragOver(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.Move;
			
		}
//-------------------------------------------------------------------------------------------------		
		void TreeView1ItemDrag(object sender, ItemDragEventArgs e)
		{
			TreeNode aNode = (TreeNode)e.Item;
			DoDragDrop(aNode, DragDropEffects.Move);
		}
//------------------пермещение файла на вкладку файлов --------------------------------------------
		void ListView1DragDrop(object sender, DragEventArgs e)
		{
			string[] handles = (string[])e.Data.GetData(DataFormats.FileDrop, false);
			foreach (string s in handles)
			{
				
				if (File.Exists(s))
				{
					
					if (addFile(treeView1.SelectedNode.Name, s))
					{
					string fileName = Path.GetFileName(s);
					ListViewItem itm = listView1.Items.Add(fileName,7);
					}
					
				}
			}
		}
//-------------------------------------------------------------------------------------------------		
		void ListView1DragEnter(object sender, DragEventArgs e)
		{
			if (e.Data.GetDataPresent(DataFormats.FileDrop)) 
				e.Effect = DragDropEffects.Copy;
		}
//---------------- Контекстное меню сохранение файла вкладки файлов -------------------------------
		void CopyToDiscToolStripMenuItemClick(object sender, EventArgs e)
		{
			ReadFile (listView1.SelectedItems[0].Name);
		}
//---------------- Контекстное меню удаление файла вкладки файлов ---------------------------------
		void DeleteToolStripMenuItem2Click(object sender, EventArgs e)
		{
			DeleteFile(listView1.SelectedItems[0].Name);
			TreeNode ns = treeView1.SelectedNode;
			read_data (ns.Name);
			toolStripStatusLabel1.Text = "Node:" + ns.Name;
			
		}
//---------------- Сохранить заметку в БД ---------------------------------------------------------		
		void Save_toolStripButton9Click(object sender, EventArgs e)
		{
				treeDB_save (db_file_name.Value);	
		}
//---------------- ????? --------------------------------------------------------------------------
		void QuestionToolStripMenuItemClick(object sender, EventArgs e)
		{
			TreeNode ns = treeView1.SelectedNode;
			set_icon (ns.Name, 8);
		}
//---------------- ????? --------------------------------------------------------------------------
		void ToolStripButton2Click(object sender, EventArgs e)
		{
			treeDB_save (db_file_name.Value);
		}
	}
}
